package com.demo.project.controller;

import com.demo.annotation.AutoWired;
import com.demo.project.entity.Account;
import com.demo.project.service.AccountService;
import com.demo.result.R;
import com.demo.web.annotation.Controller;
import com.demo.web.annotation.RequestMapping;
import com.demo.web.annotation.RequestParam;
import com.demo.web.webenum.RequestType;

import java.util.Map;

@Controller
@RequestMapping("/account")
public class AccountController {
    @AutoWired
    private AccountService accountService;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    /**
     * 注册功能
     * @param accountNumber
     * @param password
     * @param accountInfo
     * @return
     */
    @RequestMapping(value = "addAccount",type = RequestType.POST)
    public R addAccount(@RequestParam("accountNumber") String accountNumber,@RequestParam("password") String password,@RequestParam(value = "accountInfo")Account accountInfo){
        String register = accountService.register(accountNumber, password, accountInfo);
        return R.success().data("uid", register);
    }

    /**
     * 登录功能的实现，返回uid
     * @param accountNumber
     * @param password
     * @return
     */
    @RequestMapping(value = "login",type = RequestType.POST)
    public R login(@RequestParam("accountNumber") String accountNumber,@RequestParam("password") String password){
        String uidByAccountNumber = accountService.getUidByAccountNumber(accountNumber, password);
        return R.success().data("uid", uidByAccountNumber);
    }

    /**
     * 验证帐号是否重复
     * @param accountNumber
     * @param password
     * @return
     */
    @RequestMapping(value = "isRepeatAccount",type = RequestType.POST)
    public R isRepeatAccount(@RequestParam("accountNumber") String accountNumber,@RequestParam("password") String password){
        boolean repeatAccountNumber = accountService.isRepeatAccountNumber(accountNumber, password);
        return R.success().data("isRepeat", repeatAccountNumber);
    }

    /**
     * 根据uid来获得用户信息
     * @param uid
     * @return
     */
    @RequestMapping(value = "getUserInfoByUid",type = RequestType.GET)
    public R getUserInfoByUid(@RequestParam("uid") String uid){
        Account accountInfoByUid = accountService.getAccountInfoByUid(uid);
        return R.success().data("userInfo", accountInfoByUid);
    }

    /**
     * 根据帐号密码来注销账户
     * @param accountNumber
     * @param password
     * @return
     */
    @RequestMapping(value = "cancellationAccount",type = RequestType.POST)
    public R cancellationAccount(@RequestParam("accountNumber") String accountNumber,@RequestParam("password") String password){
        boolean cancellation = accountService.cancellation(accountNumber, password);
        return R.success().data("isCancel", cancellation);
    }

    @RequestMapping(value = "getAllAccounts",type = RequestType.GET)
    public R getAllAccounts(){
        Map<String, Account> allAccounts = accountService.getAllAccounts();
        return R.success().data("accounts", allAccounts);
    }

}
